package org.example.view.admin;

import org.example.controller.AdminController;
import org.example.data.*;
import org.example.view.Login;
import org.example.view.page.UpdatePassword;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.List;

public class AdminIndex extends JFrame {
    private UserWithDetails user;

    private JButton button;//一级按钮
    private String[] roomManagement = {"添加客房","修改客房"};//客房管理
    private JMenuItem[] roomMenuItem;
    private JButton userButton;//用户管理
    private JButton updatePassword;//修改密码
    private JTable roomTable;//房间信息表
    private JTable roomInTable;//入住信息表
    private static List<Room> rooms;
    private static List<RoomIn> roomIns;
    public static Boolean status = true;

    public AdminIndex(UserWithDetails user) {
        super("管理员-" + user.getUserMessage().getName() + " 欢迎来到酒店管理系统");
        this.user = user;
        JPanel panel = new JPanel();
        getButtons();//获取按钮
        initializeTables();
        getData();
        JButton exit = new JButton("退出");
        exit.addActionListener(e -> {
            SwingUtilities.invokeLater(() -> new Login().setVisible(true));
            dispose();
        });
        panel.add(button);
        panel.add(userButton);
        panel.add(updatePassword);
        panel.add(exit);

        add(panel);

        // 设置窗口大小不可调整
        this.setResizable(false);
        this.setSize(1024,768);
        // 窗口居中显示
        Dimension screen = this.getToolkit().getScreenSize();
        this.setLocation((screen.width - this.getSize().width) / 2, (screen.height - this.getSize().height) / 2);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    private void getData() {
        try {
            roomIns = new AdminController().viewRoomIns().getData();
            rooms = new AdminController().viewRooms().getData();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        updateRoomTable();
        updateRoomInTable();
    }

    private void getButtons() {
        button = new JButton("客房管理");
        roomMenuItem = new JMenuItem[2];
        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                JPopupMenu popupMenu = new JPopupMenu();
                // 二级按钮
                for (int i = 0; i < 2; i++) {
                    roomMenuItem[i] = new JMenuItem(roomManagement[i]);
                    roomMenuItem[i].addActionListener(createAction(i));
                    popupMenu.add(roomMenuItem[i]);
                }
                popupMenu.show(button, 0, button.getHeight());
            }
        });
        userButton = new JButton("用户管理");
        updatePassword = new JButton("修改密码");
        //为按钮添加点击事件
        userButton.addActionListener(e->{
            new UserManagement();
        });
        updatePassword.addActionListener(e->{
            new UpdatePassword(user.getUser());
        });
    }
    // 创建一个方法来生成ActionListener，以避免在循环内部直接使用匿名类
    private ActionListener createAction(final int index) {
        return e -> {
            switch (index) {
                case 0:
                    System.out.println("添加客房");
                    SwingUtilities.invokeLater(() -> new AddRoom(user).setVisible(true));
                    dispose(); // 关闭窗口
                    break;
                case 1:
                    System.out.println("修改客房");
                    SwingUtilities.invokeLater(() -> new UpdateRoom(user).setVisible(true));
                    dispose(); // 关闭窗口
                    break;
            }
        };
    }
    private void initializeTables() {
        // 初始化房间信息表格
        String[] columnNamesRoom = {"房间号", "类型","状态","楼层", "价格"}; // 根据实际情况调整列名
        roomTable = new JTable(new DefaultTableModel(columnNamesRoom, 0));
        JScrollPane roomScrollPane = new JScrollPane(roomTable);
        roomScrollPane.setBounds(6, 50, 500, 300); // 示例位置和大小，请按需调整
        add(roomScrollPane);

        // 初始化入住信息表格
        String[] columnNamesRoomIn = {"房间号", "客人id", "入住时间","截至时间" ,"退房时间"}; // 调整列名以匹配实际数据结构
        roomInTable = new JTable(new DefaultTableModel(columnNamesRoomIn, 0));
        JScrollPane roomInScrollPane = new JScrollPane(roomInTable);
        roomInScrollPane.setBounds(512, 50, 500, 300); // 示例位置和大小，请按需调整
        add(roomInScrollPane);
    }

    private void updateRoomTable() {
        DefaultTableModel model = (DefaultTableModel) roomTable.getModel();
        model.setRowCount(0); // 清空现有行
        for (Room room : rooms) {
            Object[] rowData = {room.getId(), room.getName(),room.getStatus(),room.getFloor(), room.getPrice()};
            model.addRow(rowData);
        }
    }

    private void updateRoomInTable() {
        DefaultTableModel model = (DefaultTableModel) roomInTable.getModel();
        model.setRowCount(0); // 清空现有行
        if (roomIns != null){
            for (RoomIn roomIn : roomIns) {
                Object[] rowData = {
                        roomIn.getRoomId(),
                        roomIn.getUserId(),
                        roomIn.getStartTime(),
                        roomIn.getEndTime(),
                        roomIn.getCheckTime()
                };
                model.addRow(rowData);
            }
        }
    }
}